package com.longxin.demoapp.experiments.experiment2.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.longxin.demoapp.R;
import com.longxin.demoapp.base.adapter.IBaseAdapter;
import com.longxin.demoapp.experiments.experiment2.controller.Experiment2AddOrEditController;
import com.longxin.demoapp.experiments.experiment2.pojo.entity.Experiment2EntityUser;
import com.longxin.demoapp.experiments.experiment2.service.Experiment2Service;
import java.util.List;

/**
 * @author Long
 * @version 1.0
 * @description:
 * @remarks:
 * @title ExperimentActivity2Adapter
 * @date 2021/4/7 11:50
 */
public class Experiment2Adapter extends IBaseAdapter<Experiment2EntityUser> {

    Experiment2Service service;

    public Experiment2Adapter(Activity activity, List<Experiment2EntityUser> datas, Experiment2Service service) {
        super(activity, datas);
        this.service = service;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //加载得到item每一项的布局
        super.init(R.layout.experiment_2_list_item);
        //得到控件
        TextView textViewUsername = (TextView) layout.findViewById(R.id.experiment_2_list_item_username);
        TextView textViewPhone = (TextView) layout.findViewById(R.id.experiment_2_list_item_phone);
        Button buttonEdit = (Button) layout.findViewById(R.id.experiment_2_list_item_btn_edit);
        Button buttonDel = (Button) layout.findViewById(R.id.experiment_2_list_item_btn_del);

        final Experiment2EntityUser user = datas.get(position);

        textViewUsername.setText(user.getUsername());
        textViewPhone.setText(user.getPhone());

        buttonEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                service.changeActivity(Experiment2AddOrEditController.class, user);
            }
        });
        buttonDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                service.remove(user);
                datas.remove(user);
                //刷新列表
                notifyDataSetInvalidated();
                //联系人数量
                service.userNum(datas.size());
            }
        });
        return layout;
    }

}
